package com.xcity.game.task.detector;

import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import com.xcity.db.entity.Build;
import com.xcity.db.entity.Player;
import com.xcity.game.task.Task;
import com.xcity.game.task.TaskDetector;
import com.xcity.game.task.TaskDetectorLabel;

/**
 * <pre>
 * 升级建筑(等级)
 * 1.建筑id(0:任意建筑)
 * 2.升级等级
 * </pre>
 * @author yang.li
 *
 */
@Deprecated
@TaskDetectorLabel(TaskDetector.UPGRADE_BUILD_LEVEL)
public class UpgradeBuildLevelTaskDetector implements TaskDetector {

	static final String KEY = String.valueOf(UPGRADE_BUILD_LEVEL);
	
	@Override
	public boolean isFinish(Task task) {
//		JSONObject builds = task.getVm().getArchive().getJSONObject(KEY);
//		if (builds == null) {
//			return false;
//		}
//		int[] conds = task.getTemplate().getConditions();
//		int buildId = conds[0];
//		int n = conds[1];
//		if (buildId != 0) {
//			return builds.getIntValue(String.valueOf(buildId)) > n;
//		}
//		Set<String> ids = builds.keySet();
//		for (String id : ids) {
//			if (builds.getIntValue(id) > n) {
//				return true;
//			}
//		}
//		return false;
		
		int[] conds = task.getTemplate().getConditions();
		int buildId = conds[0];
		int level = conds[1];
		Player player = task.getVm().getOwner();
		if (buildId != 0) {
			Build build = player.getBuildings().getBuild(buildId);
			return build != null && build.getLevel() >= level;
		}
		List<Build> buildings = player.getBuildings().getBuildings();
		for (int i = 0, size = buildings.size(); i < size; i++) {
			Build b = buildings.get(i);
			if (b.getLevel() >= level) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void onArchive(Task task, Object... vals) {
//		// { 4: { buildId: level } }
//		JSONObject builds = player.getTasks().getArchive().getJSONObject(KEY);
//		if (builds == null) {
//			builds = new JSONObject();
//			player.getTasks().getArchive().put(KEY, builds);
//		}
//		Build build = (Build) vals[1];
//		String buildId = String.valueOf(build.getId());
//		builds.put(buildId, build.getLevel());
	}

}
